package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        if(dishFlavors!=null&&dishFlavors.size()>0) {
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertBatch(dishFlavors);
        }
    }

    @Override
    public PageResult getPage(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<DishVO> dishes= dishMapper.getPage(dto);
        Page page=(Page) dishes;
        return new PageResult(page.getTotal(),page.getResult());
    }


    @Transactional
    public void deleteDishBatch(List<Long> ids) {
        /**
         * 判断：
         * 1.是否被套餐关联
         * 2.是否起售
         * 3.dish表删id对应数据
         * 4.dish_flavor表删id对应数据
         */
        ids.forEach(id->{
            Dish dis= dishMapper.selectById(id);

            if (dis.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            if (setmealDishMapper.getCountByDishId(id)>0){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }


        });
        dishMapper.deleteDishBatch(ids);
        dishFlavorMapper.deleteDishFlavorBatch(ids);

    }

    @Override
    public DishVO getConcreateDish(Long id) {
        return dishMapper.getConcreateDish(id);
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        /**
         * 1.更新dish表
         * 2.删除该菜品在dish_flavor中的所有口味
         * 3.新增当前口味
         */
        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        Long dishId= dishFlavors.get(0).getDishId();
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setId(dishId);
        dishMapper.update(dish);
        Dish oldDish=dishMapper.selectById(dishId);
        if(dishFlavorMapper.getFlavorByDishId(dishId).size()>0) {
            dishFlavorMapper.deleteFlavorByDishId(dishId);
        }

        dishFlavors.forEach(dishFlavor -> {
                    dishFlavor.setDishId(dishId);
                }
        );

        dishFlavorMapper.insertBatch(dishFlavors);






    }

    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateDishStatus(dish);
    }

    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {
        return dishMapper.getDishByCategoryId(categoryId);

    }


}
